module.exports = {
	isTencent() {
		// #ifdef H5
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/MicroMessenger/i) == "micromessenger" || ua.match(/QQ/i) == "qq") {
			return true;
		} else {
			return false;
		}
		// #endif
		// #ifndef H5
		return false;
		// #endif
	},
	toast: function(text, duration, success) {
		uni.showToast({
			title: text || "出错啦~",
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	setData: function(key, value) {
		try {
			uni.setStorageSync(key, value);
		} catch (e) {
			// error
		}
	},
	getData: function(key) {
		try {
			const value = uni.getStorageSync(key);
			if (value) {
				return value;
			}
		} catch (e) {
			// error
		}
	
	},
	setJson: function(key, value) {
		let jsonString = JSON.stringify(value);
		try {
			uni.setStorageSync(key, jsonString);
		} catch (e) {
			// error
		}
	},
	getJson: function(key) {
		try {
			const value = uni.getStorageSync(key);
			if (value) {
				return JSON.parse(value);
			}
		} catch (e) {
			// error
		}
	},
	get: function(key) { //获取队列里面全部的数据
		let data = this.getJson(key);
		if (data instanceof Array) {
			return data;
		}
		return [];
	},
	insert: function(param) { //队列插入数据
		param.capacityNum = param.capacityNum || 100; //队列容量 默认队列中超过100条数据，自动删除尾部
		let data = this.getJson(param.key);
		if (data instanceof Array) {
			if (data.length > param.capacityNum) {
				let total = data.length - param.capacityNum;
				for (let i = 0; i < total; i++) {
					data.pop();
				}
			}
			data.unshift(param.value);
		} else {
			data = [];
			data.push(param.value);
		}
		this.setJson(param.key, data);
	},
	removeItem: function(key, itemIds) { //提供itemIds数组 批量删除队列中的某项数据
		let data = this.getJson(key);
		if (data instanceof Array) {
			for (let i = 0; i < itemIds.length; i++) {
				for (let p = 0; p < data.length; p++) {
					if (itemIds[i] === data[p].itemid) {
						data.splice(p, 1);
						break;
					}
				}
			}
			this.setJson(key, data);
		}
	},
	isExist: function(key, itemId) { //检测某条数据在队列中是否存在
		let data = this.getJson(key);
		if (data instanceof Array) {
			for (let p = 0; p < data.length; p++) {
				if (itemId == data[p].itemid) {
					return true;
				}
			}
		}
		return false;
	},
	remove: function(key) { //删除某条队列
		try {
			uni.removeStorageSync(key);
			//localStorage.removeItem(key)
		} catch (e) {
			// error
		}
	},
	getCount: function(key) { //获取队列中全部数据数量
	
		let data = this.getJson(key);
		if (data instanceof Array) {
			return data.length;
		}
		return 0;
	},
	getBgColor(val){
		if (typeof val == 'string') {
			return 'linear-gradient(90deg,' + val + ',' + val + ')';
		} else if (Array.isArray(val) && val.length >= 2) {
			let navBgColor = 'linear-gradient(90deg';
			val.forEach(item => {
				if (typeof item == 'string') {
					navBgColor += ',' + item;
				} else if (typeof item == 'object') {
					navBgColor += ',' + item.color + ' ' + item.scale;
				}
			});
			navBgColor += ')';
			return navBgColor;
		}
	},
	navTo(item) {
		if (item.pdd) {
			// #ifdef MP-WEIXIN
			wx.navigateToMiniProgram({
				appId: item.link.we_app_info.app_id,
				path: item.link.we_app_info.page_path,
				envVersion: 'release'
			});
			// #endif
			// #ifdef MP-QQ
			uni.navigateToMiniProgram({
				appId: item.link.qq_app_info.app_id,
				path: item.link.qq_app_info.page_path,
				envVersion: 'release'
			});
			// #endif
			// #ifdef H5
			location.href = decodeURIComponent(item.link.web.mobile_short_url);
			// #endif
		} else {
			uni.navigateTo({
				url:item.url
			});
		}
	},
	navTo(item) {
		console.log("888888");
		if (item.pdd) {
			// #ifdef MP-WEIXIN
			wx.navigateToMiniProgram({
				appId: item.link.we_app_info.app_id,
				path: item.link.we_app_info.page_path,
				envVersion: 'release'
			});
			// #endif
			// #ifdef MP-QQ
			uni.navigateToMiniProgram({
				appId: item.link.qq_app_info.app_id,
				path: item.link.qq_app_info.page_path,
				envVersion: 'release'
			});
			// #endif
			// #ifdef H5
			location.href = decodeURIComponent(item.link.web.mobile_short_url);
			// #endif
		} else {
			console.log(item);
			if (item.url == "/pages/web/web?link=eleme") {
				wx.navigateToMiniProgram({
					appId: "wxece3a9a4c82f58c9",
					path: "pages/sharePid/web/index?scene=" + "https://s.click.ele.me/U49cAsu",
					envVersion: 'release'
				});
			} else if (item.url == "/pages/web/web?link=meituan") {
				wx.navigateToMiniProgram({
					appId: "wxde8ac0a21135c07d",
					path: "/index/pages/h5/h5?weburl=https%3A%2F%2Frunion.meituan.com%2Furl%3Fkey%3Da144a9fa40a42c55af00214bb4bb3993971%26url%3Dhttps%253A%252F%252Fi.meituan.com%252Fawp%252Fhfe%252Fblock%252Fa13b87919a9ace9cfab4%252F89400%252Findex.html%253Fappkey%253Da144a9fa40a42c55af00214bb4bb3993971%253A9456dingdanxiajiumengwaimai%26sid%3D9456dingdanxiajiumengwaimai&f_token=1&f_userId=1",
					envVersion: 'release'
				});
			} else if (item.url == "/pages/web/web?link=elemeguoshu") {
				wx.navigateToMiniProgram({
					appId: "wxece3a9a4c82f58c9",
					path: "pages/sharePid/web/index?scene=" + "https://s.click.ele.me/HYtV5su",
					envVersion: 'release'
				});
			} else {
				uni.navigateTo({
					url: item.url
				});
			}
		}
	},
	getParentId() {
		let parentId = 0;
		try {
			uni.getStorage({
				key: "parent",
				success: (res) => {
					parentId = res.data
				}
			})
		} catch (e) {
			
		}
		return parentId;
	},
	timeFormat: function(timestamp = null, fmt = 'yyyy-mm-dd') {
		// 其他更多是格式化有如下:
		// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
		timestamp = parseInt(timestamp);
		// 如果为null,则格式化当前时间
		if (timestamp == null) timestamp = Number(new Date());
		// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
		if (timestamp.toString().length == 10) timestamp *= 1000;
		let date = new Date(timestamp);
		let ret;
		let opt = {
			"y+": date.getFullYear().toString(), // 年
			"m+": (date.getMonth() + 1).toString(), // 月
			"d+": date.getDate().toString(), // 日
			"h+": date.getHours().toString(), // 时
			"M+": date.getMinutes().toString(), // 分
			"s+": date.getSeconds().toString() // 秒
			// 有其他格式化字符需求可以继续添加，必须转化成字符串
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
	},
}